package com.xxkj.drugtraceability.ui.prescriptiondrugback

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hjq.shape.layout.ShapeLinearLayout
import com.xxhis.refactor.ext.dp2px
import com.xxhis.refactor.ext.screenWidth
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DrugInfoBean
import com.xxkj.drugtraceability.bean.PatientDrugData
import com.xxkj.drugtraceability.databinding.LayoutDrugScan1Binding
import com.xxkj.drugtraceability.databinding.LayoutPrescriptiondrugcollectionBinding
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class PrescriptionDrugBackAdapter(private val actionClick:(index:Int)->Unit):BaseQuickAdapter<PatientDrugData,BaseViewHolder>(0,null) {


    override fun getItemViewType(position: Int): Int {
        return when (data.size) {
            1 -> {
                1
            }
            else -> {
                2
            }
        }
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return if (viewType==1){
            BaseViewHolder(LayoutInflater.from(context).inflate(R.layout.one_item,null))
        }else{
            BaseViewHolder(LayoutInflater.from(context).inflate(R.layout.mult_item,null))
        }
    }
    override fun convert(holder: BaseViewHolder, item: PatientDrugData) {
        holder.setText(R.id.tv_name,item.psnName)
        holder.setText(R.id.tv_cf,item.rxno)
        when (data.size) {
            2 -> {
                //需要给item设置屏幕宽度的一半
                var layoutP = holder.itemView.layoutParams as? ViewGroup.MarginLayoutParams
                if (layoutP == null) {
                    // Assuming RecyclerView's parent is a LinearLayoutManager, default to ViewGroup.LayoutParams
                    layoutP = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    holder.itemView.layoutParams = layoutP
                }
                layoutP.width =  context.screenWidth/2 -context.dp2px(20)
                layoutP.setMargins(0,0,context.dp2px(5),0)
                holder.itemView.layoutParams = layoutP
                holder.getView<LinearLayout>(R.id.ll_parent).isSelected  =item.isSelected
                holder.getView<TextView>(R.id.tv_name).isSelected  =item.isSelected
                holder.getView<TextView>(R.id.tv_middle).isSelected  =item.isSelected
                holder.getView<TextView>(R.id.tv_cf).isSelected  =item.isSelected
                holder.itemView.clickN {
                    actionClick.invoke(getItemPosition(item))
                }
            }
            1 -> {
                var layoutP = holder.itemView.layoutParams  as? ViewGroup.MarginLayoutParams
                if (layoutP == null) {
                    // Assuming RecyclerView's parent is a LinearLayoutManager, default to ViewGroup.LayoutParams
                    layoutP = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    holder.itemView.layoutParams = layoutP
                }
                layoutP.width =  context.screenWidth -context.dp2px(20)
                holder.itemView.layoutParams = layoutP

            }
            else -> {
                var layoutP = holder.itemView.layoutParams as? ViewGroup.MarginLayoutParams
                if (layoutP == null) {
                    // Assuming RecyclerView's parent is a LinearLayoutManager, default to ViewGroup.LayoutParams
                    layoutP = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    holder.itemView.layoutParams = layoutP
                }
                layoutP.width =  ViewGroup.LayoutParams.WRAP_CONTENT
                layoutP.setMargins(0,0,context.dp2px(5),0)
                holder.itemView.layoutParams = layoutP
                holder.getView<LinearLayout>(R.id.ll_parent).isSelected  =item.isSelected
                holder.getView<TextView>(R.id.tv_name).isSelected  =item.isSelected
                holder.getView<TextView>(R.id.tv_middle).isSelected  =item.isSelected
                holder.getView<TextView>(R.id.tv_cf).isSelected  =item.isSelected
                holder.itemView.clickN {
                    actionClick.invoke(getItemPosition(item))
                }
            }
        }
    }
}